<?php

namespace Yearnfar\Bundle\BlogBundle\Twig\Extension;

use CG\Core\ClassUtils;

class BlogExtension extends \Twig_Extension
{
    protected $loader;
    protected $controller;

    public function __construct(\Twig_LoaderInterface $loader)
    {
        $this->loader = $loader;
    }

    public function setController($controller)
    {
        $this->controller = $controller;
    }

    /**
     * {@inheritdoc}
     */
    public function getFunctions()
    {
        return array(
            new \Twig_SimpleFunction('info', array($this, 'getInfo'), array('is_safe' => array('html'))),
            new \Twig_SimpleFunction('user', array($this, 'getUser'), array('is_safe' => array('html'))),
            new \Twig_SimpleFunction('topMenu', array($this, 'getTopMenu'), array('is_safe' => array('html'))),
            new \Twig_SimpleFunction('sideMenu', array($this, 'getSideMenu'), array('is_safe' => array('html'))),
            new \Twig_SimpleFunction('pagination', array($this, 'getPagination'), array('is_safe' => array('html'))),
        );
    }

    public function getInfo()
    {

    }

    public function getUser()
    {
        return $this->controller[0]->getUser();
    }

    public function getTopMenu()
    {
        $div = <<<EOT
<div class="navbar-header pull-left" role="navigation">
    <ul class="nav ace-nav">
        %s
        %s
    </ul>
</div>
EOT;
        $top = <<<EOT
<li>
    <a href="/user/wechat">
        <i class="icon-user"></i>
        <span class="bigger-110 no-text-shadow"> 用户中心 </span>
    </a>
</li>
EOT;
        $li2 = <<<EOT
<li>
    <a href="/admin/user">
        <i class="icon-cog"></i>
        <span class="bigger-110 no-text-shadow"> 系统管理 </span>
    </a>
</li>
EOT;
        if (in_array('ROLE_ADMIN', $this->controller[0]->getUser()->getRoles())) {
            return sprintf($div, $top, $li2);
        } else {
            return sprintf($div, $top, "");
        }
    }

    public function getSideMenu($nav=0)
    {
        static $html = null;
        static $navMenu  = null;

        if ($html===null) {
            $dbo = $this->controller[0]->getDoctrine()->getRepository('Yearnfar\Bundle\BlogBundle\Entity\menu');
            $menus = $dbo->findAll();
            list($html, $sub) = $this->subMenu($menus, 0);
            krsort($sub);

            foreach($sub as $k => $v) {
                if ($k==0) {
                    $navMenu .= "<li class=\"active\">" . $v->getName() . "</li>";
                } else {
                    $navMenu .= "<li><a href=\"".$v->getRoute()."\">".$v->getName()."</a></li>";
                }
            }
        }

        if ($nav) {
            return $navMenu;
        } else {
            return $html;
        }
    }


    private function subMenu($menus, $mid)
    {
        static $pathinfo = null;

        if ($pathinfo===null) {
            $pathinfo = $this->controller[0]->get('request')->getPathInfo();
            $pathinfo = $pathinfo!==null ? $pathinfo : "";
        }

        $subMenu = array();
        $sub = '';
        $m = null;
        $isOpen = 0;

        foreach ($menus as $o) {
            if ($o->getPid()==$mid) {//子菜单
                list($html, $_subMenu, $_isOpen) = $this->subMenu($menus, $o->getId());
                $sub .= $html;
                $isOpen = max($isOpen, $_isOpen);
                //
                if (!empty($_subMenu)) {
                    $subMenu = $_subMenu;
                }
            } elseif ($o->getId()==$mid) {//本身
                $m = $o;
            }
        }

        $self = $sub!='' ? <<<EOT
<li class="%s">
    <a href="%s" class="dropdown-toggle">
        <i class="%s"></i>
         %s
        <b class="arrow icon-angle-down"></b>
    </a>
    <ul class="submenu" style="%s">
        %s
    </ul>
</li>
EOT
: <<<EOT
<li class="%s">
    <a href="%s">
        <i class="%s"></i>
        %s
    </a>
</li>
EOT;
        if ($m) {
            if ($isOpen==1) {
                $class = "active open";
            } elseif ($m->getRoute()==$pathinfo) {
                $class = "active";
                $isOpen = 1;
            } else {
                $class = "";
            }

            if ($isOpen) $subMenu[] = $m;

            $name = $m->getName();
            if ($m->getPid()==0) {
                $name = sprintf("<span class=\"menu-text\">%s</span>", $name);
            }

            if ($sub!='') {
                $html = sprintf($self, $class, $m->getRoute(), $m->getClass(), $name, $class=="active open"?"display: block;":"", $sub);
            } else {
                $html = sprintf($self, $class, $m->getRoute(), $m->getClass(), $name);
            }
        } else {
            $html = $sub!='' ? sprintf("<ul class=\"nav nav-list\">%s</ul>", $sub) : "";
        }

        return array($html, $subMenu, $isOpen);
    }

    public function getPagination($link, $current, $total, $offset=5)
    {
        $html = <<<EOT
            <ul class="pagination" style="margin: 5px 5px;">
                <li%s>
                    <a href="%s">
                        <i class="icon-double-angle-left"></i>
                    </a>
                </li>
                %s
                <li%s>
                    <a href="%s">
                        <i class="icon-double-angle-right"></i>
                    </a>
                </li>
            </ul>
EOT;
        $li  = <<<EOT
            <li%s><a href="%s">%d</a></li>
EOT;
        $lis = $left = $right = '';

        for ( $i=$current-$offset; $i<=$current+$offset; $i++) {
            if ($i<1) {
                $left = ' class="disabled"';
                continue;
            } elseif ($i>$total) {
                $right = ' class="disabled"';
                continue;
            } else {
                $lis .= sprintf($li, $i == $current ? ' class="active"' : '', "{$link}?page=$i", $i);
            }
        }

        return sprintf($html, $left, "{$link}?page=1", $lis, $right, "{$link}?page={$total}");

    }

    /**
     * Returns the name of the extension.
     *
     * @return string The extension name
     */
    public function getName()
    {
        return 'blog';
    }
}
